package com.hoshiicloud.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.stock.entity.Wvoucherh;
import com.hoshiicloud.stock.mapper.IWvoucherhMapper;
import com.hoshiicloud.stock.service.IWvoucherhService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 仓库单据Service接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/18 14:47
 * @mail : maiboyc@163.com
 **/
@Service
public class WvoucherhServiceImpl extends ServiceImpl<IWvoucherhMapper, Wvoucherh> implements IWvoucherhService {

    /**
     * 根据id获取
     * @param id ID
     * @return Wvoucherh
     */
    @Cacheable(cacheNames = "Wvoucherh", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "仓库管理-仓库单据管理-根据ID查询仓库单据信息")
    @Override
    public Wvoucherh getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据一组id获取
     * @param ids id组
     * @return List<Wvoucherh>
     */
    @Cacheable(cacheNames = "Wvoucherh", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#ids")
    @Logable(authName = "仓库管理-仓库单据管理-根据ID组查询仓库单据信息")
    @Override
    public List<Wvoucherh> getByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 保存
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Wvoucherh", allEntries = true)
    @Logable(authName = "仓库管理-仓库单据管理-新增仓库单据信息")
    @Override
    public Long insert(Wvoucherh entity) {
        // TODO
        // 调用供应商微服务检查供应商信息是否存在
        // 这里业务逻辑是可以是租户仓库内部单、也可以是租户平台、也可以是租户平台店铺，所以需要判断租户、平台、店铺这几个字段是否为空
        // 创建日期 修改日期 创建人 修改人 删除标识
        entity.setCreatedTime(LocalDateTime.now());
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setCreatedBy("admin");// 需要拿当前的登录人
        entity.setDeleted(0);
        //需要做日期转换
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增仓库单据信息失败"));
        }
        return entity.getId();
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Wvoucherh", allEntries = true)
    @Logable(authName = "仓库管理-仓库单据管理-修改仓库单据信息")
    @Override
    public Long update(Wvoucherh entity) throws ServiceException {
        // 自动填入修改时间 修改人
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setUpdatedBy("admin1");// 需要拿当前的登录人
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改仓库单据信息失败"));
        }
        return entity.getId();
    }

    /**
     * 查询
     * @param entity 查询参数实体
     * @return IPage<Wvoucherh>
     */
    @Cacheable(cacheNames = "Wvoucherh", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageIndex+':'+#entity.pageSize+':'+#entity.billno+':'+#entity.type+':'+#entity.deptcode+':'+#entity.vouchertype+':'+#entity.address+':'+#entity.sourcebill+':'+#entity.status")
    @Logable(authName = "仓库管理-仓库单据管理-分页查询仓库单据信息")
    @Override
    public IPage<Wvoucherh> List(Wvoucherh entity) {
        QueryWrapper<Wvoucherh> queryWrapper = new QueryWrapper();
        IPage<Wvoucherh> page = new Page();
        /*
        if (null != entity.getPageIndex()) {
            page.setCurrent(entity.getPageIndex());
        }
        if (null != entity.getPageSize()){
            page.setSize(entity.getPageSize());
        }
         */
        if(StringUtils.isNotEmpty(entity.getBillno())) {
            queryWrapper.like("billno", entity.getBillno());
        }
        if(null != entity.getType()) {
            queryWrapper.eq("type", entity.getType());
        }
        if(StringUtils.isNotEmpty(entity.getDeptcode())) {
            queryWrapper.like("deptcode",entity.getDeptcode());
        }
        if(StringUtils.isNotEmpty(entity.getVouchertype())) {
            queryWrapper.like("vouchertype",entity.getVouchertype());
        }
        if(null != entity.getAddress()) {
            queryWrapper.like("address",entity.getAddress());
        }
        if(null != entity.getSourcebill()) {
            queryWrapper.like("sourcebill",entity.getSourcebill());
        }
        if(null != entity.getStatus()) {
            queryWrapper.eq("status",entity.getStatus());
        }
        // 默认查询只查询未逻辑删除的数据
        queryWrapper.eq("deleted", 0);
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除
     * @param id ID
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Wvoucherh", allEntries = true)
    @Logable(authName = "仓库管理-仓库单据管理-删除仓库单据信息")
    @Override
    public boolean delete(Long id) throws ServiceException {
        Wvoucherh entity = this.getById(id);
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除仓库单据信息失败"));
        } else {
            return true;
        }
    }

}  